<?php  defined('BASEPATH') OR exit('No direct script access allowed');

class Header
{
    private $_config = array();
    
    private $_doctype = 'html5';
    private $_charset = 'utf-8';
    
    private $_directory = '';
    private $_sub_directory = '';
    
    private $_css_folder = 'css/';
    private $_js_folder = 'js/';
    
    private $_title = array();
    private $_seperator = ' | ';
    
    private $_js = array();
    private $_css = array();
    private $_meta = array();
    
    public  $directory = '';
    private $_ci;
    
    /**
     * Constructor - Sets Preferences
     *
     * The constructor can be passed an array of config values
     */
    public function __construct($config = array())
    {
        $this->_ci =& get_instance();
    
        if ( ! empty($config)) {
            $this->initialize($config);
        }
    
        log_message('debug', 'Header class Initialized');
    }
    
    /**
     * Initialize preferences
     *
     * @access	public
     * @param	array
     * @return	void
     */
    public function initialize($config = array())
    {
        // process each key
        foreach ($config as $key => $val) {
            if (isset($this->{'_'.$key})) {
                if (is_array($this->{'_'.$key})) {
                    if (!is_array($val)) {
                        $val = array($val);
                    }
                    $this->{'_'.$key} = array_merge($this->{'_'.$key}, $val);
                } else {
                    $this->{'_'.$key} = $val;
                }
            } elseif (is_array($val)) {
                $this->_config[$key] = $val;
            }
        }
        
        // set final directory for public path
        $this->directory = $this->_directory . $this->_sub_directory;
    }
    
    /**
     * Append title to page title.
     *
     * @param string $title
     */
    public function add_title($title)
    {
        $this->_title[] = $title;
    }
    
    /**
     * Generate header array based on config file header.php.
     *
     * If a $name is passed, take extra config $name to merge to the common config.
     *
     * $header['doctype'] contains doctype tag (<!DOCTYPE>)
     * $header['meta']    contains all meta tags (<meta>)
     * $header['js']      contains all javascript tags (<script>)
     * $header['css']     contains all css tags (<link>)
     * $header['title']   contain title tag (<title>)
     * $header['header']  contain title, js, css and meta tags.
     *
     * @param string $name
     * @return array
     */
    public function generate($name = null)
    {
        
        if ($name != null && key_exists($name, $this->_config)) {
            $this->initialize($this->_config[$name]);
        }
        
        $header = array();
        
        // get doctype
        if ($this->_doctype != '') {
            $header['doctype'] = $this->doctype($this->_doctype);
        }
        
        if ($this->_charset != '') {
            $header['meta'] = $this->charset($this->_charset);
        }
        
        if (!empty($this->_meta)) {
            $header['meta'] .= $this->meta($this->_meta);
        }
        
        if (!empty($this->_css)) {
            $header['css'] = $this->css($this->_css);
        }
        
        if (!empty($this->_js)) {
            $header['js'] = $this->js($this->_js);
        }
        
        if (!empty($this->_title)) {
            $header['title'] = $this->title($this->_title, $this->_seperator);
        }
        
        $header['header'] = $header['title'] . $header['css'] . $header['js'] . $header['meta'];
        
        return $header;
    }
    
    /**
     * Return a title tag <title></title>
     *
     * @param array!string $title
     * @param string $seperator
     * @return html
     */
    public function title($title, $seperator = ' ')
    {
        if (is_array($title)) {
            $title = implode($seperator, $title);
        }
        
        return '<title>' . $title . '</title>' . "\n";
    }
    
    /**
     * Return javascript tags <script src=""></script>
     *
     * If $use_config_path is true, use the path from config file header.php.
     *
     * @param array!string $js
     * @param boolean $use_config_path
     * @return html
     */
    public function js($js, $use_config_path = true)
    {
        if ($use_config_path != false) {
            $js_path = $this->_ci->config->slash_item('base_url') . $this->directory . $this->_js_folder;
        }
        
        $extra =  ($this->_doctype != 'html5') ? ' type="text/javascript"' : '';
        
        $html = '';
        
        if (is_array($js)) {
            foreach ($js as $js_file) {
                if ($pos = strpos($js_file, '<!--')) {
                    $html .= substr($js_file, $pos) . "\n";
                    $js_file = trim(substr($js_file, 0, $pos));
                }
                if (strpos($js_file, 'http://') === false) {
                    $js_file = $js_path . $js_file;
                }
                $html .= '<script src="' . $js_file . '"' . $extra . ' /></script>'."\n";
            }
        } else {
            if ($pos = strpos($js, '<!--')) {
                $html .= substr($js, $pos) . "\n";
                $js = trim(substr($js, 0, $pos));
            }
            if (strpos($js_file, 'http://') === false) {
                $js = $js_path . $js;
            }
            $html .= '<script src="' . $js . '"' . $extra . ' /></script>'."\n";
        }
        
        return $html;
    }
    
    /**
     * Return css tags <link href=""></link>
     *
     * If $use_config_path is true, use the path from config file header.php.
     *
     * @param array|string $css
     * @param boolean $use_config_path
     * @return html
     */
    public function css($css, $use_config_path = true)
    {
        if ($use_config_path != false) {
            $css_path = $this->_ci->config->slash_item('base_url') . $this->directory . $this->_css_folder;
        }
        
        $extra = ' rel="stylesheet"';
        $extra .=  ($this->_doctype != 'html5') ? ' type="text/css"' : '';
        
        $html = '';
        
        if (is_array($css)) {
            foreach ($css as $css_file) {
                if ($pos = strpos($css_file, '<!--')) {
                    $html .= substr($css_file, $pos) . "\n";
                    $css_file = trim(substr($css_file, 0, $pos));
                }
                if (strpos($css_file, 'http://') === false) {
                    $css_file = $css_path . $css_file;
                }
                $html .= '<link href="' . $css_file . '"' . $extra . ' />'."\n";
            }
        } else {
            if ($pos  = strpos($css, '<!--')) {
                $html .= substr($css, $pos) . "\n";
                $css  = trim(substr($css, 0, $pos));
            }
            if (strpos($css, 'http://') === false) {
                $css = $css_path . $css;
            }
            $html .= '<link href="' . $css . '"' . $extra . ' />'."\n";
        }
        
        return $html;
    }
    
    /**
     * Return charset tag <meta charset=""></meta>
     *
     * @param string $charset
     * @return html
     */
    public function charset($charset)
    {
        return '<meta charset="' . $charset . '" />' . "\n";
    }
    
    /**
     * Return meta tags <meta type="" name=""></meta>
     *
     * @param array $meta
     * @return html
     */
    public function meta($meta)
    {
        if (!isset($meta[0])) {
            $meta = array($meta);
        }
        
        $html = '';
        foreach ($meta as $element) {
            $type	 = (!isset($element['type']))    ? 'name' : $element['type'];
            $name	 = (!isset($element['name']))    ? ''     : $element['name'];
            $content = (!isset($element['content'])) ? ''     : $element['content'];
            $comment = (!isset($element['comment'])) ? ''     : $element['comment'] . "\n";
            
            $html .= $comment . '<meta '.$type.'="'.$name.'" content="'.$content.'" />'."\n";
        }

        return $html;
    }
    
    /**
     * Return doctype tag <!DOCUMENT>
     *
     * @param string $type
     * @return html
     */
    public function doctype($type)
    {
        if (is_array($type)) {
            show_error('Doctype cannot be an array.');
        }
        
        include(APPPATH.'config/doctypes.php');
        
        return (isset($_doctypes[$type])) ? $_doctypes[$type] : false;
    }
}

?>